
// 获取当前时间时间戳（到日）
function todayTimestamp() {
	return Date.parse(new Date(new Date().setHours(0, 0, 0, 0))) / 1000
}

// 今天时间戳（到日）
export const today = { 
	date: new Date(new Date().setHours(0, 0, 0, 0)),
	timestamp: todayTimestamp()
}

// data转时间戳
export function dateToTimestamp(date) {
	return Date.parse(date) / 1000
}

// 提前 num 天(Date格式)
export function beforeDate(num) {
	return new Date(today.date - num * 24 * 60 * 60 * 1000)
}

// 提前 num 天(时间戳格式)
export function beforeTimestamp(num) {
	return today.timestamp - 86400 * num
}

// 用于结束日期，精确到当日23:59:59
export function lastSecond(timestamp) {
	return timestamp + (86400 - 1)
}

export function parseTime(time, cFormat) {
  if (arguments.length === 0) {
    return null
  }

  if ((time + '').length === 10) {
    time = +time * 1000
  }

  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    date = new Date(parseInt(time))
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key]
    if (key === 'a') return ['一', '二', '三', '四', '五', '六', '日'][value - 1]
    if (result.length > 0 && value < 10) {
      value = '0' + value
    }
    return value || 0
  })
  return time_str
}